<template>
  <div id="reg">
    <div class="left">
      <div class="bbb"><b>注册</b></div>
      <p>在我们这里创建一个帐户，您将能够：</p>
      <ul>
        <li>更快的结账</li>
        <li>保存更多的收货地址</li>
        <li>访问你的浏览记录</li>
        <li>跟踪新的订单</li>
        <li>在你的购物车中保存商品</li>
      </ul>
    </div>
    <div class="right">
      <el-form
        status-icon
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        label-position="top"
      >
        <el-form-item label="设置登录账号" prop="account">
          <el-input
            v-model="ruleForm.account"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="设置用户名" prop="username">
          <el-input
            v-model="ruleForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            立即注册
          </el-button>
          <el-button @click="resetForm('ruleForm')"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        account: "",
        username: "",
        password: "",
        checkPass: "",
      },
      rules: {
        account: [{ required: true, message: "请设置登录账号" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 15, message: "长度在  到 15 个字符", trigger: "blur" },
        ],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios
            .request({
              url: "/api" + "/user/reg",
              method: "post",
              data: {
                account: this.ruleForm.account,
                password: this.ruleForm.password,
                username: this.ruleForm.username
              },
            })
            .then((res) => {
              if (!res.data) {
                this.$message.error("注册失败，卡了");
              } else {
                console.log(res);
                this.$message.success("注册成功");
                setTimeout(()=>{
                  this.$router.push("/login")
                },1000)
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>


<style scoped>
#reg {
  margin: 0 auto;
  display: flex;
  width: 1200px;
  justify-content: space-between;
  overflow: hidden;
}
.el-input {
  width: 650px;
}
</style>